<template>
  <div class="app-container">
    <el-tree :data="roots" node-key="id" default-expand-all :expand-on-click-node="false" style="padding: 20px">
      <template #default="{ node, data }">
        <div>
          <span>{{ data.code }}&nbsp;&nbsp;&nbsp;&nbsp;{{ data.remark }}&nbsp;&nbsp;&nbsp;&nbsp;</span>
          <Authority :authorities="['access:insert']">
            <el-button :icon="icons.Plus" type="text" @click="append(data)">Append</el-button>
          </Authority>
          <Authority :authorities="['access:delete']">
            <el-button class="remove" :icon="icons.Delete" type="text" @click="remove(node, data)">Delete</el-button>
          </Authority>
        </div>
      </template>
    </el-tree>

    <el-dialog title="添加权限" width="25%" v-model="visible" :before-close="setVisible">
      <el-form label-width="80px" label-position="left">
        <el-form-item label="权限编码">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span>
          <el-button @click="setVisible">取 消</el-button>
          <el-button type="primary" @click="insert">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
  import { defineComponent } from 'vue';
  import { AuthorityNode, useAuthority } from '../hooks/useAuthority';
  import { useVisibleForm } from '@/hooks/useDialogForm';
  import { Access, accessService } from '@/services/authorization/Access';
  import { ElMessage, ElMessageBox } from 'element-plus';
  import { HttpUtil } from '@/utils/HttpUtil';

  import { Plus, Delete } from '@element-plus/icons-vue';

  export default defineComponent({
    name: 'tree',
    setup: () => {
      const { select, roots } = useAuthority();

      const { form, setVisible, visible } = useVisibleForm<Partial<Access>>({
        code: undefined,
        remark: undefined,
      });

      const append = (data: AuthorityNode) => {
        setVisible();
        form.parent = data.id;
      };

      const remove = async (node: unknown, data: AuthorityNode) => {
        try {
          await ElMessageBox.confirm('此操作将删除该权限, 是否继续?', '警告', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          });
        } catch (e) {
          return;
        }
        try {
          const resp = await accessService.deleteById(data.id);
          if (HttpUtil.isNoContent(resp)) {
            ElMessage.success('删除权限成功!');
          }
        } catch (e) {
          ElMessage.error((e as Error).message);
        } finally {
          await select();
        }
      };

      const insert = async () => {
        try {
          const resp = await accessService.insert(form);
          if (HttpUtil.isCreated(resp)) {
            ElMessage.success('添加权限成功!');
          }
        } catch (e) {
          ElMessage.error((e as Error).message);
        } finally {
          setVisible();
          await select();
        }
      };
      return {
        roots,
        append,
        remove,
        form,
        setVisible,
        visible,
        insert,
        icons: {
          Plus,
          Delete,
        },
      };
    },
  });
</script>

<style lang="scss" scoped>
  @import '~@/assets/styles/index.scss';

  .remove {
    color: $red;
  }
</style>
